/*
 * auto_test.c
 *
 * Created: 2012/11/21 23:02:11
 *  Author: Kiyotaka Akasaka
 */

#include "include/auto_test.h"

#define AUTO_TEST_TTY UART_transmitString

typedef BOOL (*unit_test_function)(void);

unit_test_function unit_test_sequence[]
=
{
	unit_test_AllocateAndDMA,
	NULL
};


/* ------ 0. Test Interface ------*/
BOOL unit_test(void)
{
	int i;
	int successCount = 0;
	
	AUTO_TEST_TTY(UART_DBG, (BYTE *)"AuteTest: Start\r\n");

	for(i=0 ; unit_test_sequence[i] != NULL ;++i){
		if(unit_test_sequence[i]() == TRUE){
			successCount++;
		}
	}
	
	AUTO_TEST_TTY(UART_DBG, (BYTE *)"AuteTest: Finished\r\n");
	
	if(successCount == sizeof(unit_test_sequence)){
		return TRUE;
	}else{
		return FALSE;
	}
}

/* ------ 1. Test For Memory System ------ */
volatile static BOOL dma_finished_flag;
void dma_callback(BOOL result)
{
	dma_finished_flag = TRUE;
	
	return;
}

BOOL unit_test_AllocateAndDMA(void)
{
	volatile DWORD srcAddress;
	volatile DWORD dstAddress;
	volatile WORD i;
	
	/* --- 1. Allocate Memory --- */
	srcAddress = Memory_allocate(512 * 2);
	dstAddress = Memory_allocate(512 * 2);
	
	AUTO_TEST_TTY(UART_DBG, (BYTE *)"unit_test_AllocateAndDMA() allocated.\r\n");
	MemoryDump(srcAddress, 16);
	MemoryDump(dstAddress, 16);
	
	/* --- 2. Create Test Pattern and Fill Blank --- */
	for(i=0 ; i<(WORD)1024L ; ++i){
		EXTBUS_write(srcAddress + i, (BYTE)(i&0xFF));
		EXTBUS_write(dstAddress + i, (BYTE)(0));
	}
	MemoryDump(srcAddress, 256);
	MemoryDump(dstAddress, 256);
		
	/* --- 3. Start DMA --- */
	AUTO_TEST_TTY(UART_DBG, (BYTE *)"unit_test_AllocateAndDMA() Start DMA.\r\n");

	dma_finished_flag = FALSE;
	Memory_startDMA(srcAddress, dstAddress, 512 * 2, dma_callback);
	
	/* --- 4. Wait for DMA complete --- */
	AUTO_TEST_TTY(UART_DBG, (BYTE *)"unit_test_AllocateAndDMA() Wait DMA.\r\n");
	while(FALSE == dma_finished_flag);
	AUTO_TEST_TTY(UART_DBG, (BYTE *)"unit_test_AllocateAndDMA() End DMA.\r\n");
	
	/* --- 5. Check Result --- */
	MemoryDump(srcAddress, 256);
	MemoryDump(dstAddress, 256);
	
	
	Memory_free(srcAddress);
	Memory_free(dstAddress);

	return TRUE;
}

